//
//  NSString+Rect.m
//  SFASalesReport
//
//  Created by Jonas on 16/8/24.
//  Copyright © 2016年 Jonas. All rights reserved.
//

#import "NSString+Rect.h"

@implementation NSString (Rect)

+ (CGSize)boundingRectWithText:(NSString *)text font:(CGFloat)font size:(CGSize)size {
    
    NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
    para.lineBreakMode = NSLineBreakByCharWrapping;
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:font],
                          NSParagraphStyleAttributeName : para};
    
    CGSize rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return rect;
}

- (NSComparisonResult)compareVersion:(NSString *)version {
    NSArray *localVersions = [self componentsSeparatedByString:@"."];
    NSArray *remoteVersions = [version componentsSeparatedByString:@"."];
    NSUInteger count = MIN(localVersions.count, remoteVersions.count);
    for (int i = 0; i < count; i++) {
        int localVersion = [localVersions[i] intValue];
        int remoteVersion = [remoteVersions[i] intValue];
        
        if (localVersion > remoteVersion) {
            return NSOrderedDescending;
        } else if (localVersion < remoteVersion) {
            return NSOrderedAscending;
        }
    }
    if (localVersions.count > remoteVersions.count) {
        return NSOrderedDescending;
    } else if (localVersions.count < remoteVersions.count) {
        return NSOrderedAscending;
    }
    
    return NSOrderedSame;
}

+ (NSMutableAttributedString *)changeTextColor:(NSString*)string OneColor:(UIColor *)oneColor TwoColor:(UIColor *)twoColor Range:(NSRange)range {
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    
    [attrString addAttribute:NSForegroundColorAttributeName value:oneColor range:range];
    
    [attrString addAttribute:NSForegroundColorAttributeName value:twoColor range:range];
    return attrString;
    
}

@end
